package com.baizan.daka.ui.fragment

import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.baizan.daka.base.BasePageListFragment
import com.baizan.daka.event.HomeEvent
import com.baizan.daka.event.TaskEvent
import com.baizan.daka.glide.ImageLoadHelp
import com.baizan.daka.mvp.model.bean.BannerBean
import com.baizan.daka.mvp.model.bean.NoticeInfoBean
import com.baizan.daka.mvp.model.bean.TaskInfoBean
import com.baizan.daka.mvp.model.bean.UserPackageDes
import com.baizan.daka.net.BasePageResponse
import com.baizan.daka.showToast
import com.baizan.daka.ui.activity.*
import com.baizan.daka.ui.adapter.TaskAdapter
import com.baizan.daka.ui.contract.HomeFrgContract
import com.baizan.daka.ui.presenter.HomeFrgPresenter
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.frg_home.*
import org.greenrobot.eventbus.EventBus
import java.util.*

class HomeFragment : BasePageListFragment<HomeFrgPresenter, TaskAdapter>(), HomeFrgContract.View {

    override fun initPresenter(): HomeFrgPresenter {
        return HomeFrgPresenter(this)
    }

    override fun layoutId(): Int {
        return R.layout.frg_home
    }

    override fun onRefresh() {
        presenter?.loadList(mCurrentPageNo)
    }

    override fun onLoadMore() {
        presenter?.loadList(mCurrentPageNo)
    }

    override fun initData() {
        super.initData()
        adapter?.setOnItemClickListener { adapter, _, position ->
            val item = adapter.data.get(position)
            item as TaskInfoBean
            presenter?.receiveTask(position, item.id)
        }
        service_tv.setOnClickListener {
            presenter?.loaduserPackageDes()

        }
        vips_tv.setOnClickListener {
            EventBus.getDefault().post(HomeEvent(2))
        }
        task1_tv.setOnClickListener {
            context?.let {
                TaskHomeActivity.start(it, 1)
            }
        }
        task2_tv.setOnClickListener {
            context?.let {
                TaskHomeActivity.start(it, 2)
            }
        }
        task3_tv.setOnClickListener {
            context?.let {
                TaskHomeActivity.start(it, 3)
            }
        }
        task4_tv.setOnClickListener {
            context?.let {
                TaskHomeActivity.start(it, 4)
            }
        }
        news_tv.setOnClickListener {
            context?.let {
                NewsListActivtity.start(it)
            }
        }
        friend_tv.setOnClickListener {
            context?.let {
                ExtensionActivity.start(it)
            }
        }
    }

    fun initViewFlipper(data: List<NoticeInfoBean>) {
        data.forEach {
            var view = layoutInflater.inflate(R.layout.item_viewflipper, null) as TextView
            view.text = it.content
            view_flipper.addView(view)
        }
//        view_flipper.setOnClickListener {
//            val index = view_flipper.displayedChild
//            val id = data.get(index).id
//            if (id == 0L) {
//                return@setOnClickListener
//            }
//            context?.let {
//                NewsDesActivity.start(it, id!!)
//            }
//        }
        if (data.isNotEmpty()) {
            view_flipper.setFlipInterval(3000)
            view_flipper.startFlipping()
        }
    }

    override fun initView() {
    }

    override fun start() {
        presenter?.loadList(mCurrentPageNo)
        presenter?.loadNewsList()
        presenter?.loadBanner()
    }


    override fun initAdapter(): TaskAdapter = TaskAdapter()
    override fun loadListSuccess(data: BasePageResponse<TaskInfoBean>) {
        getDatasSuccess(data)
    }

    override fun loadListFail(fail: String) {
        getDatasFail()
    }

    override fun loadNewsListSuccess(data: List<NoticeInfoBean>) {
        if (data.isEmpty()) {
            loadNewsListFail()
        } else {
            initViewFlipper(data)
        }
    }

    override fun loadNewsListFail() {
        val list = ArrayList<NoticeInfoBean>()
        list.add(NoticeInfoBean())
        initViewFlipper(list)
    }

    override fun loadBannerSuccess(data: List<BannerBean>) {
        data.isEmpty().let {
            banner.visibility = if (it) View.GONE else View.VISIBLE
        }
        banner.run {
            setAutoPlayAble(data.size > 2)
            setData(data, null)
            setAdapter { banner, _, bannerInfo, position ->
                var bannerBean = bannerInfo as BannerBean
                ImageLoadHelp.loadImage(
                    banner.getItemImageView(position), bannerBean.bannerUrl
                )

            }
        }
    }

    override fun loadBannerFail() {
        banner.visibility = View.GONE
    }

    override fun receiveTaskSuccess(index: Int, taskInfoBean: Any) {
        adapter?.setReceived(index)
        EventBus.getDefault().post(TaskEvent())
        showToast("领取成功")
    }

    override fun loadUserPackageDesSuccess(userPackageDes: UserPackageDes) {
        context?.let {
            PublishTaskActivity.start(it, userPackageDes)
        }
    }

    override fun loadUserFial(error: String) {
        mContext?.let {
            var dialog = AlertDialog.Builder(it)
            dialog.setTitle("提示")
            dialog.setMessage(error)
            dialog.setPositiveButton("确定") { dialog, which ->
                FlowPackageActivity.start(it)
            }
            dialog.setNegativeButton("取消") { dialog, which ->
                dialog.dismiss()
            }
            dialog.create().show()
        }

    }


}